/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Server.SQL;

import Server.Helper.Analyzer;
import Server.Helper.StringHelper;
import java.util.Vector;
import Server.MyDataType.MyDataDynamic;

/**
 *
 * @author CANG
 */
public class CommandManage 
{
    
    String _sCommand = "";
    public CommandManage(String command)
    {
        _sCommand = command;
    }
    
    public boolean ExecuteNonQuery(String sCommand)
    {
        boolean bExecute = false;
        MyDataDynamic data = new MyDataDynamic();
        CommandType commandType = Analyzer.AnalysisType(_sCommand);
        MyCommand command = null;
        
        MyCommand.splitFirstString(_sCommand, " ");
        if(commandType == CommandType.INSERT)
        {
             command = new InsertCommand(_sCommand);
            data = ((InsertCommand)command).analysic(_sCommand);
        }
        
        if(commandType == CommandType.DELETE)
        {
             command = new DeleteCommand(_sCommand);
            data = ((DeleteCommand)command).Analysis(_sCommand);
        }
        
        if(commandType == CommandType.UPDATE)
        {
             command = new UpdateCommand(_sCommand);
            data = ((UpdateCommand)command).Analysis(_sCommand);
        }
        if(data != null)
            return bExecute;
        else return false;
    }
    
    public MyDataDynamic ExecuteQuery(String sCommand)
    {
        boolean bExecute = true;
        CommandType commandType = Analyzer.AnalysisType(_sCommand);                
        int index_space = _sCommand.indexOf(" ");
        _sCommand = _sCommand.substring(index_space + 1, _sCommand.length());
        MyCommand command = null;
        
        command = new SelectCommand(_sCommand);
        MyDataDynamic myData =  ((SelectCommand)command).Analysic_SelectCommand(_sCommand);
        return myData;
    }
    
    
    public static boolean isNonQuery(String command)
    {       
        if(Analyzer.AnalysisType(command) != CommandType.SELECT)
            return true;
        return false;
    }
     public static String getType(String command)
    {       
        switch(Analyzer.AnalysisType(command))
        {
            case SELECT:
                return "select";
            case INSERT:
                return "insert";
            case DELETE:
                return "delete";
            case UPDATE:
                return "update";
            default:
                return "unknow";
        }
    }   
    public MyDataDynamic Analysis()
    {
        MyDataDynamic data = new MyDataDynamic();
        try
        {
            if(Analyzer.AnalysisLevel(_sCommand) == 2)
            {
                data = AnalysicQueryLevel2();
            }
            else
            {
                data = AnalysicQueryLevel1();
            }
        }
        catch(Exception e)
        {
            return null;
        }
        
        return data;
    }
    
    public MyDataDynamic AnalysicQueryLevel1()
    {
        
        boolean bExecute = true;
        MyDataDynamic data = new MyDataDynamic();
        
        CommandType commandType = Analyzer.AnalysisType(_sCommand);
        MyCommand command = null;
        
        MyCommand.splitFirstString(_sCommand, " ");
        if(commandType == CommandType.DELETE)
        {
            command = new DeleteCommand(_sCommand);
            data = ((DeleteCommand)command).Analysis(_sCommand);
        }
        
        if(commandType == CommandType.INSERT)
        {
            command = new InsertCommand(_sCommand);
            data = ((InsertCommand)command).analysic(_sCommand);
        }
        
        if(commandType == CommandType.SELECT)
        {
            command = new SelectCommand(_sCommand);
            data = ((SelectCommand)command).Analysic_SelectCommand(_sCommand);
        }
        
        if(commandType == CommandType.UPDATE)
        {
            command = new UpdateCommand(_sCommand);
            data = ((UpdateCommand)command).Analysis(_sCommand);
        }
        
        return data;
        //loai bo type comand
    }

    public MyDataDynamic AnalysicQueryLevel2()
    {
        MyDataDynamic data = new MyDataDynamic();
        
        try
        {
            Analyzer ana = new Analyzer();
            // Kiểm tra câu lệnh cấp 2
            if(ana.AnalysisLevel(_sCommand) != 2)
                return null;

            // Tách câu lệnh cấp 2.
            String cmdLevel2 = new StringHelper().GetQueryLevel2(_sCommand);

            // Thực hiện câu lệnh cấp 2.
            int index_space = cmdLevel2.indexOf(" ");
            cmdLevel2 = cmdLevel2.substring(index_space + 1, cmdLevel2.length());
            MyCommand command = null;

            command = new SelectCommand(cmdLevel2);
            MyDataDynamic resultLevel2 = ((SelectCommand)command).Analysic_SelectCommand(cmdLevel2);

            // Chuyển đổi kết quả thực hiện câu lệnh cấp 2 thành String
            // Chuyền về theo định dạng của IN ( A,B,C );
            String attName = new StringHelper().AttributeOfIn(_sCommand);
            Vector<String> lstValue = resultLevel2.CreateListValueOfAttribute(attName);
            String in = "";
            for(int i = 0; i<lstValue.size()-1; i++)
            {
                in += "'"+lstValue.get(i) +"',";
            }
            in += "'"+lstValue.get(lstValue.size() - 1) +"'";
            // Chuyển cấu lệnh đưa vào về định dạng câu lệnh cấp 1 với output của câu lệnh cấp 2.
            int indexQueryLevel2 = new StringHelper().IndexQueryLevel2(_sCommand);
            String[] lstStrings = _sCommand.split(" ");
            Vector<String> lstFirt = new Vector<>();
            for(int i = 0; i<lstStrings.length; i++)
            {
                lstFirt.add(lstStrings[i]);
            }
            for(int i = lstFirt.size() - 2; i> indexQueryLevel2; i--)
            {
                lstFirt.remove(i);
            }
            lstFirt.set(indexQueryLevel2, in);
            // Thực hiện câu lệnh cuối cùng.
            String queryEnd = "";
            for(int i = 0; i<lstFirt.size(); i++)
            {
                queryEnd += lstFirt.get(i) + " ";
            }
            queryEnd = queryEnd.trim();

            _sCommand = queryEnd;
            data = AnalysicQueryLevel1();
        }
        catch(Exception e) 
        {
            return null;
        }
        return data;
    }
    
}
